package com.xyazm.coupon.api;

import com.xyazm.common.core.JsonResult;
import com.xyazm.coupon.domain.dto.UserCouponDTO;
import com.xyazm.coupon.domain.param.QueryUserCouponParam;
import com.xyazm.coupon.domain.param.ReleaseUserCouponParam;
import com.xyazm.coupon.domain.param.UseUserCouponParam;
import com.xyazm.coupon.exception.CouponBizException;
import com.xyazm.coupon.service.IUserCouponService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 用户券
 * @author xuyou
 * @date 2022年06月04日 10:59
 */
@Slf4j
@DubboService(version = "1.0.0",interfaceClass = IUserCouponApi.class)
public class UserCouponApi implements IUserCouponApi {

    @Autowired
    private IUserCouponService userCouponService;


    @Override
    public JsonResult<UserCouponDTO> getUserCoupon(QueryUserCouponParam param) {
        try {
            UserCouponDTO userCouponDTO = userCouponService.getUserCoupon(param);
            return JsonResult.buildSuccess(userCouponDTO);
        } catch (CouponBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<Void> useUserCoupon(UseUserCouponParam param) {
        try {
            userCouponService.useUserCoupon(param);
            return JsonResult.buildSuccess();
        } catch (CouponBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<Void> releaseUserCoupon(ReleaseUserCouponParam param) {
        try {
            userCouponService.releaseUserCoupon(param);
            return JsonResult.buildSuccess();
        } catch (CouponBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
